package com.css.fxfzxczh.modules.fieldWorkManage.lnvestigationTaskMange.repository.impl;

import com.css.fxfzxczh.constants.YNEnum;
import com.css.fxfzxczh.modules.fieldWorkManage.lnvestigationTaskMange.entity.InvestigationTask;
import com.css.fxfzxczh.modules.fieldWorkManage.lnvestigationTaskMange.repository.InvTaskManageRepository;
import com.css.fxfzxczh.modules.fieldWorkManage.lnvestigationTaskMange.vo.InvestigationTaskVo;
import com.css.fxfzxczh.modules.fieldWorkManage.surTaskManage.entity.SurTaskManage;
import com.css.fxfzxczh.util.PlatformObjectUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:
 * @CreateTime: 2023/10/24 16:36
 * @Description:
 */
@Repository
public class InvTaskManageRepositoryImpl implements InvTaskManageRepository {
    @Resource
    JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public Map<String, Object> getPage(int curPage, int pageSize, String xsemId) {
        StringBuilder sql = new StringBuilder("select *,ST_AsText(geom) geom from xczh_investigation_task_manage");
        StringBuilder whereSql = new StringBuilder(" where del_flag = '0'");
        StringBuilder orderSql = new StringBuilder(" order by create_time desc");
        int firstIndex = (curPage - 1) * pageSize;
        int lastIndex = pageSize;
        StringBuilder limitSql = new StringBuilder(" limit "+lastIndex+" offset "+ firstIndex);
        if (!PlatformObjectUtils.isEmpty(xsemId)) {
            whereSql.append(" and xsem_id = '"+xsemId+"'");
        }

        sql.append(whereSql).append(orderSql);
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(SurTaskManage.class)).size();
        List<InvestigationTask> list = jdbcTemplate.query(sql.append(limitSql).toString(), new BeanPropertyRowMapper<>(InvestigationTask.class));
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("rows", list);
        return map;
    }

    @Override
    public List<InvestigationTaskVo> getTaskList(String xsemId,String taskType) {
        StringBuilder sql = new StringBuilder("select *,ST_AsText(geom) geom from xczh_investigation_task_manage");
        StringBuilder whereSql = new StringBuilder(" where del_flag = '0'");
        StringBuilder orderSql = new StringBuilder(" order by create_time desc");
        if (!PlatformObjectUtils.isEmpty(xsemId)) {
            whereSql.append(" and xsem_id = '"+xsemId+"'");
        }
        if (!PlatformObjectUtils.isEmpty(taskType)) {
            whereSql.append(" and task_Type = '"+taskType+"'");
        }
        sql.append(whereSql).append(orderSql);
        List<InvestigationTaskVo> list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(InvestigationTaskVo.class));



//        String sql = "select *,ST_AsText(geom) geom from xczh_investigation_task_manage where del_flag = '0' " +
//                "and xsem_id = '"+xsemId+"' order by create_time desc";
//        List<InvestigationTaskVo> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(InvestigationTaskVo.class));
        return list;
    }

    @Override
    public void save(InvestigationTask investigationTask) {
        String sql = "insert into xczh_investigation_task_manage(xitm_id,xsem_id,xsem_code,task_name," +
                "group_id,group_name,task_status,create_user,create_time,update_user,update_time,del_flag,task_code," +
                "task_range,geom,create_user_name,task_type,group_number,quadrant_number) " +
                "values(:xitmId, :xsemId, :xsemCode, :taskName, " +
                ":groupId, :groupName, :taskStatus, :createUser, :createTime, :updateUser, :updateTime, " +
                ":delFlag, :taskCode,:taskRange,ST_GeomFromText(:geom,4490),:createUserName,:taskType,:groupNumber,:quadrantNumber)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(investigationTask);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public void update(InvestigationTask investigationTask) {
        String sql = "update xczh_investigation_task_manage set task_name = :taskName,group_id= :groupId, " +
                "group_name = :groupName ,update_user = :updateUser ,update_time = :updateTime, " +
                "geom = ST_GeomFromText(:geom,4490) where xitm_id = :xitmId";
        SqlParameterSource source = new BeanPropertySqlParameterSource(investigationTask);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public InvestigationTask findById(String id) {
        String sql = "select *,ST_AsText(geom) geom from xczh_investigation_task_manage where xitm_id = ? ";
        List<InvestigationTask> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(InvestigationTask.class), id);
        return query.size()<1?null:query.get(0);
    }

    @Override
    public void deleteById(List<String> id) {
        String sql = "update xczh_investigation_task_manage set del_flag = :flag  where xitm_id in (:ids)";
        Map idMap = new HashMap();
        idMap.put("ids", id);
        idMap.put("flag", YNEnum.Y.toString());
        namedParameterJdbcTemplate.update(sql,idMap);
    }

    @Override
    public String getMaxTaskCode(String code) {
        String sql="select max(task_code) from xczh_investigation_task_manage where del_flag = '0' and task_code like '%"+code+"%'";
        String maxNum=jdbcTemplate.queryForObject(sql,String.class);
        return maxNum;
    }

    @Override
    public void batchSave(List<InvestigationTask> list) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(list.toArray());
        String sql = "insert into xczh_investigation_task_manage(xitm_id,xsem_id,xsem_code,task_name," +
                "group_id,group_name,task_status,create_user,create_time,update_user,update_time,del_flag,task_code," +
                "task_range,geom,create_user_name,task_type,extend4,extend5) " +
                "values(:xitmId, :xsemId, :xsemCode, :taskName, " +
                ":groupId, :groupName, :taskStatus, :createUser, :createTime, :updateUser, :updateTime, " +
                ":delFlag, :taskCode,:taskRange,ST_GeomFromText(:geom,4490),:createUserName,:taskType,:extend4,:extend5)";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }

    @Override
    public void updateTaskStatu(String xitmId, String taskStatus) {
        String sql = "update xczh_investigation_task_manage set task_status = '"+taskStatus+"'  where xitm_id = '"+xitmId+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void deleteByXsemId(String xsemId) {
        String sql = "update xczh_investigation_task_manage set del_flag = ?  where xsem_id = ?";
        jdbcTemplate.update(sql,YNEnum.Y.toString(),xsemId);
    }
}
